< previous page page_136 next page >

Page 136
Although this might be okay for a project that has only two text box controls on it, think if your application had 10 or more text box controls on a form. You can see how this would very quickly bog down your coding process, especially if you are changing several properties for the controls. By using a control array and a processing loop, you can quickly set the properties for a set of controls. The following code will set the BackColor property for every text box that is included in the control array:
Dim txtObject as Object
For each txtObject in txtInputLine
    txtObject.BackColor = lblTxtBack.BackColor
Next
As you can see from this simple example, this technique lets you work with the control array without having to know how many elements there are, or having to reference each one separately.
Creating Your Own Class
If you have been programming for any length of time, you know that most programmers start to collect a library of functions and routines that can be used in any application that requires them. In Visual Basic, you can define your own data types to meet certain application design requirements. You can use classes in much the same way. Although classes don't replace functions in your application code, they do provide a convenient way to organize those routines and the data they use. If you looked closely at any of the object classes that are included in Visual Basic, such as a text box, you would see that the class combines data and procedure code into one succinct unit.
Although you don't want to create a class for every routine you write, you can create a class out of it if the routine is going to be useful in many different applications. This will enable you to add the class to an application if you need it. To see how to add a class to your application, you first must create one. For the purposes of this lesson, you are going to create a small class that will calculate the age of a person based on his birthdate, and then if the age is greater than an age limit that is passed to the class, an event will be triggered to allow the application to act on that information. Although classes can become very complicated, the creation process is the same no matter what type of class procedure you create.
Note
Don't confuse creating a class with creating a custom control. Both are classes, but a custom control can have a form interface associated with it, whereas a class module does not.

 
< previous page page_136 next page >

If you like this book, buy it!